﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ModelLibrary
{
    public class RepairInfo : clsBaseLib
    {
        public RepairInfo()
        {
            base.connectDataBase();
        }
        public RepairInfo(string sAppSet)
        {
            base.connectDataBase(sAppSet);
        }
        #region 设备维修申请列表

        public DataTable getRespairAppList(int iPageId, int iDeptId, string sWhere)
        {
            int iStartRow = (iPageId - 1) * iPageRowNum + 1;
            int iEndRow = iPageId * iPageRowNum;
            string sDept = iDeptId == 0 ? @"" : string.Format(" and c.Deptid in(select deptid from department where dbo.getDeptFullPath(deptid) like dbo.getDeptFullPath({0})+'%')", iDeptId);
            sWhere = sWhere == "" ? "" : "and " + sWhere;
            string sSql = string.Format(@"select * from
            (
            select row_number() over(order by a.ID desc) as rowindex,  c.DeviceCode, c.DeviceName, c.Devicespec, c.DeptId, a.*
FROM         dbo.Device AS c INNER JOIN
                      dbo.Department AS b ON c.DeptId = b.DeptId INNER JOIN
                      dbo.T_RepairPlanAppNote AS a ON c.DeviceId = a.DevID
            where 1=1 and  {0}
            {1}
            ) t
            where rowindex between {2} and {3}", sDept, sWhere, iStartRow, iEndRow);
            return db.GetTable(sSql);
        }

        public int getRepairAppCount(int iDeptId, string sWhere)
        {
            string sDept = iDeptId == 0 ? @"" : string.Format(" and c.deptid in(select DeptId from department where dbo.getDeptFullPath(deptid) like dbo.getDeptFullPath({0})+'%')", iDeptId);
            sWhere = sWhere == "" ? "" : "and " + sWhere;
            string sSql = string.Format(@"select count(ID)
            FROM         dbo.Device AS c INNER JOIN
                      dbo.Department AS b ON c.DeptId = b.DeptId INNER JOIN
                      dbo.T_RepairPlanAppNote AS a ON c.DeviceId = a.DevID
                where 1=1{0}{1}", sDept, sWhere);
            return Convert.ToInt32(db.ExecuteScalar(sSql));
        }

        public bool deleteRepairAppByIds(string sIds)
        {
            string sSql = string.Format(@"delete from T_RepairPlanAppNote where ID in({0}) 
            delete from T_RepairPlanItem where PlanID in({0}) delete from T_RepairPlanPartList where PlanID in({0})", sIds);
            return db.ExecuteNonQuery(sSql, true) > 0;
        }

        #endregion
    }
}
